﻿using BatchPrint.Services;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Input;

namespace BatchPrint;

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    private readonly MainWindowViewModel _viewModel;

    public MainWindow(MainWindowViewModel viewModel)
    {
        _viewModel = viewModel;
        DataContext = viewModel;
        InitializeComponent();
        this.Loaded += MainWindow_Loaded;
        this.container.MouseWheel += Container_MouseWheel;
    }

    private void Container_MouseWheel(object sender, MouseWheelEventArgs e)
    {
        var maxWidth = 2000;
        var minWidth = 210;
        double width = container.Width;
        if (e.Delta > 0)
            width += 30;
        else
            width -= 30;

        if (width >= minWidth && width <= maxWidth)
        {
            container.Width = width;
            container.Height = container.Width * 1.414285714285714;
        }
    }

    private void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        //_viewModel.Canvas = this.canvas;
        _viewModel.Container = container;
        this.SizeChanged += _viewModel.WindowSizeChanged;
        this.KeyDown += _viewModel.WindowKeyChange;
        this.KeyUp += _viewModel.WindowKeyChange;

    }

    private void ListView_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
    {
        _viewModel.WindowSelectionChanged(sender, e);
    }

    private void batchBtn_Click(object sender, RoutedEventArgs e)
    {
        //NativeMethods.ShellExecute(IntPtr.Zero, "print", "C:\\Users\\yuanh\\Documents\\报销\\20241019\\1008 29.74元.pdf", null, null, 0);

        //var imagePath = "C:\\Users\\yuanh\\Documents\\报销\\20241019\\10.08加班.jpg";

        //PrintDocument pd = new PrintDocument();
        //pd.PrintPage += (sender, args) =>
        //{
        //    System.Drawing.Image img = System.Drawing.Image.FromFile(imagePath); //Image.FromFile(filePath);
        //    args.Graphics.DrawImage(img, args.MarginBounds);
        //};
        //pd.Print();

        var printManager = new PrinterManager();

        var names = printManager.GetAvailablePrinters();
        MessageBox.Show(string.Join(", ", names));
    }

    private void ListView_MouseUp(object sender, MouseButtonEventArgs e)
    {
        Debug.WriteLine(e.Source);
    }
}

public class NativeMethods
{
    [DllImport("shell32.dll", CharSet = CharSet.Auto)]
    public static extern IntPtr ShellExecute(
        IntPtr hwnd,
        string lpOperation,
        string lpFile,
        string lpParameters,
        string lpDirectory,
        int nShowCmd);
}